import {message} from 'antd';
import {formatMessage} from './intl';

export const newOrUpdateOrderValidator = data => {
    const requiredFields = [];
    if(!data.code)
        requiredFields.push(formatMessage({
            id: 'validator.message.code',
            defaultMessage: '编号'
        }));
    if(!data.name)
        requiredFields.push(formatMessage({
            id: 'validator.message.name',
            defaultMessage: '名称'
        }));
    if(!data.shortCode)
        requiredFields.push(formatMessage({
            id: 'validator.message.shortCode',
            defaultMessage: '简码'
        }));
    if(!data.areaTiledId)
        requiredFields.push(formatMessage({
            id: 'validator.message.areaTiledId',
            defaultMessage: '国省市'
        }));
    if(!data.kind)
        requiredFields.push(formatMessage({
            id: 'validator.message.kind',
            defaultMessage: '客户分类'
        }));
    if(!data.type)
        requiredFields.push(formatMessage({
            id: 'validator.message.type',
            defaultMessage: '客户类型'
        }));
    if(!/^[A-Z]{2}$/.test(data.shortCode)) {
        message.warning(`${formatMessage({
            id: 'validator.message.validateShortCode',
            defaultMessage: '"简码"字段应只能输入两个大字字母'
        })}`);
        return false;
    }
    if(requiredFields.length > 0) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.message.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }

    return true;
};

